﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.OA.Utils;
using Ccit.Utils;
using System.Collections.Generic;
using Ccit.OA.MsPersonDoc;
using System.Xml;
using System.Text;

namespace Ccit.OA.PersonDoc
{
    public class PersonDocSQL : IPersonDoc
    {
        #region 获取空间设置信息
        /// <summary>
        /// 获取空间设置信息
        /// </summary>
        /// <param name="sQuery"></param>
        /// <returns></returns>
        public DataSet GetPersonDocSpaceMess(string sQuery)
        {
            if (sQuery != "")
            {
                sQuery = sQuery.Replace("$$userName$$", "userName");
                sQuery = sQuery.Replace("$$DeptName$$", "DeptName");
                sQuery = sQuery.Replace("$$PostName$$", "postionname");
                sQuery = sQuery.Replace("$$Archives$$", "DOC_A3_20");
                sQuery = sQuery.Replace("$$Already$$", "DOC_A2_60");
            }
            //string cmdText = "select DOC_A3_10 as SId,userName,DeptName,postionname as PostName,DOC_A3_20 as Archives,";
            //cmdText += "convert(decimal(18,2),isnull((DOC_A2_60/1024)/1024,0)) as Already,";
            //cmdText += "convert(decimal(18,2),isnull(DOC_A3_20,0)-isnull((DOC_A2_60/1024)/1024,0))as Residual";
            //cmdText += "  from DOC_A3 left  join DOC_A2 on DOC_A3_10 = DOC_A2_20";
            //cmdText += " left join v_udp on userId = DOC_A3_10";
            //cmdText += " where 1=1 "+sQuery+"";
            string cmdText;

            cmdText = "SELECT A.SId,A.userName,A.DeptName,A.PostName,A.Archives,A.Already,(A.Archives-A.Already) AS Residual ";
            cmdText += "FROM ";
            cmdText += "( ";
            cmdText += "select userId as SId,userName,DeptName,postionname as PostName,isnull(DOC_A3_20,0) as Archives, ";
            cmdText += "isnull(cast((select sum(doc_a2_60)/1024/1024 as zw from doc_a2 where DOC_A2_20 = userId) as decimal(38,2)),.00) as Already ";
            cmdText += "from v_udp left join DOC_A3 on userId = doc_a3.doc_a3_10 where 1=1 ";
            if (sQuery != "")
            {
                cmdText += sQuery;
            }
            cmdText += ") A ";

            DataSet dSet = DataAccessor.ExecuteDataSet(cmdText);
            return dSet;
        }

        #endregion

        #region 返回所有文件夹详细信息
        /// <summary>
        /// 返回所有文件夹详细信息
        /// </summary>
        /// <param name="Ms">文件夹消息对象</param>
        /// <param name="sQuery">查询条件</param>
        /// <returns></returns>
        public DataSet GetPersonDocfolder(MsFolder Ms, string sQuery, string sType)
        {
            string cmdText;
            DataSet dSet = new DataSet();
            if (Ms.UserId.Trim() != "")
            {
                //不属于文件编辑状态时候

                if (sType == "")
                {
                    cmdText = "select *,(select cast(DOC_A1_110 as varchar(500)) FROM DOC_A1 WHERE DOC_A1_10 = A.DOC_A1_30) AS url,USERNAME from DOC_A1 A ";
                    cmdText += "left join v_udp on  USERID = DOC_A1_40 ";
                }
                else
                {
                    cmdText = "select *,(select cast(DOC_A1_110 as varchar(500)) FROM DOC_A1 WHERE DOC_A1_10 = A.DOC_A1_30) AS url,USERNAME from DOC_A1 A ";
                    cmdText += "left join v_udp on  USERID = DOC_A1_40 ";
                    cmdText += "WHERE 1=1 " + sQuery;
                }
                dSet = DataAccessor.ExecuteDataSet(cmdText);
            }
            return dSet;
        }
        #endregion

        #region 设置空间大小
        /// <summary>
        /// 设置空间大小
        /// </summary>
        /// <param name="Ms"></param>
        /// <returns></returns>
        public List<string> DoOperateSpace(MsPersonDocSpace Ms)
        {
            List<string> lstResult = new List<string>();
            List<string> lstCmdText = new List<string>();
            List<string> lstCount = new List<string>();
            string cmdText = "";
            ConfigLib oConfig = new ConfigLib();
            string PERSONALSPACE = oConfig.GetAttribute("CCIT_SYSTEM/CCIT_PERSONALSPACE", eValueType.TEXT);//空间大小
            if (Ms.UserId != "")
            {
                string[] flowid = Ms.UserId.Split(',');
                lstCount = GetSpace(Ms.UserId);
                if (lstCount.Count > 0)
                {
                    for (int i = 0; i < lstCount.Count; i++)
                    {
                        for (int j = 0; j < flowid.Length; j++)
                        {
                            if (lstCount[i] == flowid[j])
                            {
                                cmdText = " update DOC_A3 set DOC_A3_20=" + Ms.SpaceSize + " where DOC_A3_10 in(" + flowid[j] + ") ";
                                lstCmdText.Add(cmdText);
                                break;
                            }
                            else
                            {

                                cmdText = " insert into DOC_A3(DOC_A3_10,DOC_A3_20) values(" + flowid[j] + "," + PERSONALSPACE + ")";
                                lstCmdText.Add(cmdText);
                                cmdText = " update DOC_A3 set DOC_A3_20=" + Ms.SpaceSize + " where DOC_A3_10 in(" + flowid[j] + ")";
                                lstCmdText.Add(cmdText);
                                break;
                            }
                        }
                    }
                }
                else
                {
                    for (int j = 0; j < flowid.Length; j++)
                    {
                        cmdText = " insert into DOC_A3(DOC_A3_10,DOC_A3_20) values(" + flowid[j] + "," + PERSONALSPACE + ")";
                        lstCmdText.Add(cmdText);
                        cmdText = " update DOC_A3 set DOC_A3_20=" + Ms.SpaceSize + " where DOC_A3_10 in(" + flowid[j] + ")";
                        lstCmdText.Add(cmdText);
                    }
                }
            }
            else
            {
                lstCount = GetUserId();
                cmdText = " delete from DOC_A3";
                lstCmdText.Add(cmdText);
                for (int i = 0; i < lstCount.Count; i++)
                {
                    cmdText = " insert into DOC_A3(DOC_A3_10,DOC_A3_20) values(" + lstCount[i] + "," + PERSONALSPACE + ")";
                    lstCmdText.Add(cmdText);
                    cmdText = "update DOC_A3 set DOC_A3_20=" + Ms.SpaceSize + " ";
                    lstCmdText.Add(cmdText);
                }
            }
            oConfig = null;
            int iNum = DataAccessor.ExecuteNonQuery(lstCmdText);
            if (iNum > 0)
            {
                lstResult.Add("1");
                lstResult.Add("保存成功!");
            }
            else
            {
                lstResult.Add("-999");
                lstResult.Add("服务器忙请稍候再试!");
            }
            return lstResult;
        }
        private List<string> GetSpace(string userId)
        {
            List<string> lstResult = new List<string>();
            string cmdText = "select DOC_A3_10,DOC_A3_20 from DOC_A3 where DOC_A3_10 in(" + userId + ")";
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                while (iReader.Read())
                {
                    lstResult.Add(iReader["DOC_A3_10"].ToString());
                }
            }
            return lstResult;
        }
        private string GetSpaceUser(string userId)
        {
            string iuserId = "";
            string cmdText = "select DOC_A3_10,DOC_A3_20 from DOC_A3 where DOC_A3_10 in(" + userId + ")";
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                if (iReader.Read())
                {
                    iuserId = iReader["DOC_A3_10"].ToString();
                }
            }
            return iuserId;
        }
        private List<string> GetUserId()
        {
            List<string> lstResult = new List<string>();
            string cmdText = "select userId from v_user ";
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                while (iReader.Read())
                {
                    lstResult.Add(iReader["userId"].ToString());
                }
            }
            return lstResult;
        }
        #endregion

        #region 删除文件
        public MsFolder DelFiles(MsFiles Ms)
        {
            bool bFlag = false;
            string sSql = string.Empty;
            string sFoldID = "";
            MsFolder msfolder = new MsFolder();
            /*单独删除一行记录*/
            if (Ms.fileType == "")
            {
                msfolder.FId = Ms.Id.Trim();
                msfolder = DelFolder(msfolder);
            }
            else
            {
                string[] IdList = Ms.Id.Split(',');
                string[] TypeList = Ms.fileType.Split(',');
                for (int i = 0; i < IdList.Length; i++)
                {
                    if (TypeList[i] != "")
                    {
                        sSql = "Delete DOC_A2 WHERE DOC_A2_10 = " + IdList[i];
                        if (DataAccessor.ExecuteNonQuery(sSql) > -1)
                        {
                            sSql = "Delete DOC_A4 WHERE DOC_A4_10 = " + IdList[i];
                            DataAccessor.ExecuteNonQuery(sSql);
                        }
                    }
                    else
                    {
                        msfolder.FId = IdList[i].Trim();
                        msfolder = DelFolder(msfolder);
                        sFoldID += msfolder.FId + ",";
                    }
                }
                msfolder.FId = sFoldID.Trim(',');
            }
            return msfolder;
        }
        #endregion

        #region 删除文件夹及文件夹下边的所有文件

        public MsFolder DelFolder(MsFolder Ms)
        {
            bool bFlag = false;
            string sSql;
            int nCount = 0;
            sSql = "select * FROM DOC_A1 WHERE DOC_A1_10 =" + Ms.FId;
            DataView dv = new DataView(DataAccessor.ExecuteDataSet(sSql).Tables[0]);

            string higfolderid = Ms.FId;
            string sConnectID = "";
            GetLowLevFolderID(higfolderid, ref sConnectID);
            #region 存在子节点时
            if (sConnectID.Trim(',') != "")
            {
                sSql = "Select DOC_A2_10 from DOC_A2 where DOC_A2_30 in(" + sConnectID.Trim(',') + ")";
                DataSet dSet = DataAccessor.ExecuteDataSet(sSql);
                nCount = dSet.Tables[0].Rows.Count;
                if (nCount != 0)
                {
                    for (int i = 0; i < nCount; i++)
                    {
                        sSql = "Delete from DOC_A2 where DOC_A2_10 =" + dSet.Tables[0].Rows[i]["DOC_A2_10"].ToString().Trim();
                        DataAccessor.ExecuteNonQuery(sSql);
                        sSql = "Delete DOC_A4 WHERE DOC_A4_10 = " + dSet.Tables[0].Rows[i]["DOC_A2_10"].ToString().Trim();
                        DataAccessor.ExecuteNonQuery(sSql);
                    }
                }
                sSql = "Delete from DOC_A1 WHERE DOC_A1_10 in (" + sConnectID.Trim(',') + ")";
                DataAccessor.ExecuteNonQuery(sSql);
            }
            #endregion
            Ms.FId = sConnectID.Trim(',');
            return Ms;
        }
        #endregion

        #region 插入文件信息
        public MsFiles insertFile(MsFiles Ms)
        {
            string sSql;
            Ms.Id = Ccit.OA.Utils.DataInterface.GetMaxId("DOC_A2", "DOC_A2_10").ToString().Trim();
            sSql = "insert into DOC_A2 values(" + Ms.Id;
            sSql += " ,'" + Ms.userId;
            sSql += "' ,'" + Ms.folderId;
            sSql += "' ,'" + Ms.fileName;
            sSql += "' ,'" + Ms.fileType;
            sSql += "' ,'" + Ms.filelength;
            sSql += "' ,'" + Ms.remark;
            sSql += "' ,'" + Ms.updatetime;
            sSql += "', '" + Ms.agencId;
            sSql += "', '" + Ms.deptId;
            sSql += "' ,'" + Ms.jobId;
            sSql += "' ,'" + Ms.levelId;
            sSql += "' ,'" + Ms.cjuserId;
            sSql += "','" + Ms.cjdatetime + "')";
            DataAccessor.ExecuteNonQuery(sSql);
            return Ms;
        }
        #endregion

        #region 保存文件夹信息

        /// <summary>
        /// 保存文件夹信息

        /// </summary>
        /// <param name="ms"></param>
        /// <returns></returns>
        public List<string> DoSaveFloderMess(MsFolder ms)
        {
            List<string> lstResult = new List<string>();
            string cmdText = "";
            string curYear = DateTime.Now.ToString("yyyy-MM-dd");
            LoginUser loginUser = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
            string sType = "-999";
            if (ms.FId == "")
            {
                #region 文件夹路径

                if (ms.Superiorid == "0")
                {
                    ms.Url = "我的文档";
                }
                else
                {
                    DataSet dSet = GetPersonDocfolder(ms, " and  DOC_A1_10 = " + ms.Superiorid, "1");
                    if (dSet.Tables[0].Rows.Count != 0)
                    {
                        ms.Url = dSet.Tables[0].Rows[0]["DOC_A1_110"].ToString().Trim() + "/" + dSet.Tables[0].Rows[0]["DOC_A1_20"].ToString().Trim();
                    }
                }
                #endregion
                ms.FId = DataInterface.GetMaxId("DOC_A1", "DOC_A1_10").ToString();
                cmdText = "insert into DOC_A1 values(" + ms.FId + ",'" + ms.FName.Replace(",", "'") + "','" + ms.Superiorid + "'" +
                    "," + ms.UserId + "," + ms.Sharingmark + ",'" + ms.ReadId + "','" + ms.Readmark + "','" + ms.CompleteyId + "','" + ms.CompleteyMark + "'" +
                    ",'" + curYear + "','" + loginUser.OrganId + "','" + loginUser.DeptId + "','" + loginUser.PosId + "','" + loginUser.ClassId + "','" + loginUser.UserId + "','" + curYear + "','" + ms.Url + "')";
                //新增
                sType = "1";
            }
            else
            {

                #region 文件夹路径

                if (ms.Superiorid == "0")
                {
                    ms.Url = "我的文档";
                }
                else
                {
                    DataSet dSet = GetPersonDocfolder(ms, " and  DOC_A1_10 = " + ms.Superiorid, "1");
                    if (dSet.Tables[0].Rows.Count != 0)
                    {
                        ms.Url = dSet.Tables[0].Rows[0]["DOC_A1_110"].ToString().Trim() + "/" + dSet.Tables[0].Rows[0]["DOC_A1_20"].ToString().Trim();
                    }
                }
                #endregion
                cmdText = "update DOC_A1 set DOC_A1_20='" + ms.FName.Replace(",", "'") + "',DOC_A1_40=" + ms.UserId + ",DOC_A1_50=" + ms.Sharingmark + "," +
                    "DOC_A1_60='" + ms.ReadId + "',DOC_A1_70='" + ms.Readmark + "',DOC_A1_80='" + ms.CompleteyId + "',DOC_A1_90='" + ms.CompleteyMark + "'" +
                    ",DOC_A1_100='" + curYear + "',DOC_A1_110 = '" + ms.Url + "'  where DOC_A1_10=" + ms.FId;
                //更新
                sType = "2";
            }
            int iNum = DataAccessor.ExecuteNonQuery(cmdText);
            if (iNum > 0)
            {
                lstResult.Add(ms.FId);
                lstResult.Add("操作成功!");

            }
            else
            {
                lstResult.Add("-999");
                lstResult.Add("服务器忙请稍候再操作!");
            }

            MsFolder msfolder = GetFolder(ms);

            lstResult.Add(sType);   //更新或增加

            lstResult.Add(msfolder.FName);    //文件名称
            lstResult.Add(msfolder.Superiorid);    //上级文件ID
            msfolder = null;
            ms = null;
            return lstResult;
        }

        #endregion

        #region  新增附件
        public MsDocVersion InsertVersion(MsDocVersion Ms, string presentId)
        {
            string sSql = string.Empty;
            sSql = " SELECT * FROM DOC_A4 WHERE DOC_A4_10 =" + presentId;
            IDataReader iReader = DataAccessor.ExecuteReader(sSql);
            if (iReader.Read())
            {
                sSql = "INSERT INTO DOC_A4(DOC_A4_10,DOC_A4_20,DOC_A4_30) VALUES(#sId,#sVersion,#sContent)";

                IDataParameter guid = DataAccessor.GetDataParameter("sId", DataType.Int);
                IDataParameter version = DataAccessor.GetDataParameter("sVersion", DataType.Int);
                IDataParameter content = DataAccessor.GetDataParameter("sContent", DataType.Binary);

                guid.Value = Ms.Id;
                version.Value = Ms.versionId;
                content.Value = (Byte[])iReader["doc_a4_30"];

                IDataParameter[] parameters = new IDataParameter[] { guid, version, content };
                DataAccessor.ExecuteNonQuery(sSql, parameters);
            }

            return Ms;
        }
        #endregion

        #region 文件信息
        public DataSet GetFilesMess(MsFiles Ms, string sType)
        {
            string sSql;
            sSql = "select * from (";
            sSql += "select Doc_A1_10 AS ID,DOC_A1_20 AS Title,'' AS FileType,null AS Length,DOC_A1_100 AS Updatetime,cast(DOC_A1_110 as varchar(500)) AS Url  from DOC_A1 ";
            sSql += "where DOC_A1_30 in (" + Ms.folderId + ") and DOC_A1_40 =" + Ms.userId;
            sSql += " union ";
            sSql += "select Doc_A2_10 AS ID,DOC_A2_40 AS Title,DOC_A2_50 AS FileType,DOC_A2_60 AS Length,DOC_A2_80 AS Updatetime,(select cast(DOC_A1_110 as varchar(500)) from Doc_a1 where DOC_A1_10 = DOC_A2_30) AS Url  from DOC_A2 where DOC_A2_30 = " + Ms.folderId;
            sSql += " and DOC_A2_20 =" + Ms.userId + " and doc_a2_30 = " + Ms.folderId;
            sSql += ") A ";
            sSql += " order by A.FileType,A.Updatetime ";
            DataSet dSet = DataAccessor.ExecuteDataSet(sSql);
            return dSet;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sQueryFies">文件查询条件</param>
        /// <param name="sQueryFolder">文件夹查询条件</param>
        /// <returns></returns>
        public DataSet GetFilesSearch(string sQuery)
        {
            LoginUser loginUser = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
            XmlDocument oDom = new XmlDocument();
            oDom.LoadXml(sQuery);
            string TitleName = oDom.GetElementsByTagName("TitleName")[0].InnerText;
            string Rang = oDom.GetElementsByTagName("Range")[0].InnerText;
            string StateTime = oDom.GetElementsByTagName("StateTime")[0].InnerText;
            string EndState = oDom.GetElementsByTagName("EndState")[0].InnerText;
            string BigSize = oDom.GetElementsByTagName("BigSize")[0].InnerText;
            string SmallSize = oDom.GetElementsByTagName("SmallSize")[0].InnerText;

            string sSql;
            #region 查询条件中不存在文件大小 查询内容从文件库与文件夹库同时进行刷选

            if (BigSize == "" && SmallSize == "")
            {
                sSql = "select Doc_A2_10 AS ID,DOC_A2_40 AS Title,DOC_A2_50 AS FileType,DOC_A2_60 AS Length,DOC_A2_80 AS Updatetime,(select cast(DOC_A1_110 as varchar(500))  from Doc_a1 where DOC_A1_10 = DOC_A2_30) AS Url ";
                sSql += " from DOC_A2 ";
                sSql += " where 1=1 ";
                #region 标题查询
                if (TitleName != "")
                {
                    sSql += " and DOC_A2_40 like '%" + TitleName + "%' ";
                }
                #endregion
                #region 时间段查询

                if (StateTime != "" && EndState != "")
                {
                    sSql += " and (DOC_A2_80 BETWEEN '" + StateTime + "' and '" + EndState + "')";
                }
                #endregion
                #region 文档类型查询
                //在我的文档类查询
                if (Rang == "1")
                {
                    sSql += " and DOC_A2_20 =" + loginUser.UserId;
                }
                //他人共享
                else if (Rang == "2")
                {
                    sSql += " and DOC_A2_20 <>" + loginUser.UserId + " and (select DOC_A1_50 from DOC_A1 WHERE DOC_A1_10 = DOC_A2.DOC_A2_30 ) = '1' ";
                }
                else
                {
                    sSql += " and (DOC_A2_20 =" + loginUser.UserId + " or (select DOC_A1_50 from DOC_A1 WHERE DOC_A1_10 = DOC_A2.DOC_A2_30 ) = '1' )";
                }
                #endregion
                sSql += " union ";
                sSql += "select Doc_A1_10 AS ID,DOC_A1_20 AS Title,'' AS FileType,null AS Length,DOC_A1_100 AS Updatetime,cast(DOC_A1_110 as varchar(500)) AS Url from DOC_A1 ";
                sSql += " where 1=1 ";
                #region 标题查询
                if (TitleName != "")
                {
                    sSql += " and DOC_A1_40 like '%" + TitleName + "%' ";
                }
                #endregion
                #region 时间段查询

                if (StateTime != "" && EndState != "")
                {
                    sSql += " and (DOC_A1_100 BETWEEN '" + StateTime + "' and '" + EndState + "')";
                }
                #endregion
                #region 文档类型
                //在我的文档类查询
                if (Rang == "1")
                {
                    sSql += " and DOC_A1_40 =" + loginUser.UserId;
                }

                //他人共享
                else if (Rang == "2")
                {
                    sSql += " and DOC_A1_40 <>" + loginUser.UserId + " and DOC_A1_50 = '1' ";
                }
                else
                {
                    sSql += " and (DOC_A1_40 =" + loginUser.UserId + " or DOC_A1_50 = '1' )";
                }

                //sSql += " order by DOC_A2.DOC_A2_80 ";
                #endregion

            }
            #endregion
            #region 当查询条件中存在文件大小时 查询内容只从文件库里边进行刷选

            else
            {
                sSql = "select Doc_A2_10 AS ID,DOC_A2_40 AS Title,DOC_A2_50 AS FileType,DOC_A2_60 AS Length,DOC_A2_80 AS Updatetime,(select cast(DOC_A1_110 as varchar(500))  from Doc_a1 where DOC_A1_10 = DOC_A2_30) AS Url from DOC_A2 ";
                sSql += " where 1=1 ";
                if (StateTime != "undefined" && EndState != "undefined")
                {
                    sSql += " and (DOC_A2_80 BETWEEN '" + StateTime + "' and '" + EndState + "')";
                }
                if (BigSize != "undefined" && SmallSize != "undefined")
                {
                    sSql += " AND (DOC_A2_80 BETWEEN '" + SmallSize + "' AND '" + BigSize + "')";
                }
                else if (BigSize != "undefined" && SmallSize == "undefined")
                {
                    sSql += " AND (DOC_A2_60 <= " + BigSize + ")";
                }
                else if (SmallSize != "undefined" && BigSize == "undefined")
                {
                    sSql += " AND (DOC_A2_60 >= " + SmallSize + ")";
                }
                if (TitleName != "")
                {
                    sSql += " and DOC_A2_40 like '%" + TitleName + "%' ";
                }
                sSql += " order by DOC_A2.DOC_A2_80 ";
            }
            #endregion
            DataSet dSet = DataAccessor.ExecuteDataSet(sSql);
            return dSet;
        }
        #endregion

        #region 文件剪切或复制

        /// <summary>
        /// 文件剪切或复制

        /// </summary>
        /// <param name="sFiesId">文件ID串 ,号分隔</param>
        /// <param name="sWork">操作类型 Code复制/Shear剪切</param>
        /// <param name="sType">文件类型串 ,号分隔</param>
        /// <param name="sObjectID">要进行 粘贴的文件夹ID</param>
        public List<string> Fiesmove(string sFiesId, string sWork, string sType, string sObjectID, string sUserId)
        {
            List<string> lstResult = new List<string>();
            string sSql, sConnectID = "", sReturn = "00";
            string[] sFiesIdList = sFiesId.Split(',');
            string[] sTypeList = sType.Split(',');
            int len = sFiesIdList.Length;
            if (len != 0)
            {
                for (int i = 0; i < len; i++)
                {
                    /*文件时 */
                    #region 剪切或复制文件时
                    if (sTypeList[i].Trim() != "")
                    {
                        MsFiles msfiles = GetFiles(sFiesIdList[i].Trim());
                        msfiles.folderId = sObjectID; //上级文件夹ID
                        msfiles.updatetime = System.DateTime.Now.ToString();
                        msfiles.userId = sUserId;   //文件所属用户ID
                        MsDocVersion msversion = new MsDocVersion();
                        msversion.versionId = "1";
                        msversion.Id = sFiesIdList[i].Trim();

                        /*文件复制*/
                        if (sWork == "Code")
                        {

                            msfiles = insertFile(msfiles);
                            msversion.Id = msfiles.Id;
                            InsertVersion(msversion, sFiesIdList[i].Trim());
                        }
                        /*文件剪切*/
                        if (sWork == "Shear")
                        {
                            sSql = "update DOC_A2 SET DOC_A2_20 = " + sUserId + ", DOC_A2_30 = " + sObjectID + " WHERE DOC_A2_10 =" + sFiesIdList[i].Trim();
                            DataAccessor.ExecuteNonQuery(sSql);
                        }
                    }
                    #endregion
                    #region 剪切或复制文件夹
                    else
                    {
                        MsFolder msfolder = new MsFolder();
                        msfolder.FId = sFiesIdList[i].Trim();
                        msfolder = GetFolder(msfolder);
                        msfolder.Superiorid = sObjectID;    //要粘贴的目录ID号

                        msfolder.UserId = sUserId;

                        sConnectID = "";
                        GetLowLevFolderID(sFiesIdList[i].Trim(), ref sConnectID);  //获取下级目录的所有ID号

                        sConnectID = "," + sConnectID.Trim(',') + ",";
                        #region 判断 文件夹是否被粘贴或复制到它的子目录中，若是操作失败

                        if (sConnectID.IndexOf("," + sObjectID + ",") == -1)
                        {

                            /*文件夹 复制*/
                            if (sWork == "Code")
                            {

                                //DoSaveFloderMess(msfolder);
                                sSql = "select DOC_A1_10 FROM DOC_A1 WHERE DOC_A1_10 =" + sFiesIdList[i].Trim();
                                DataSet dSet = DataAccessor.ExecuteDataSet(sSql);
                                DataView dv = new DataView(dSet.Tables[0]);
                                CodeFolder(dv, sUserId, sObjectID);
                            }
                            if (sWork == "Shear")
                            {
                                DataSet dSet = GetPersonDocfolder(msfolder, " and  DOC_A1_30 = " + msfolder.Superiorid, "1");
                                if (dSet.Tables[0].Rows.Count != 0)
                                {
                                    msfolder.Url = dSet.Tables[0].Rows[0]["DOC_A1_110"].ToString().Trim() + "/" + dSet.Tables[0].Rows[0]["DOC_A1_20"].ToString().Trim();
                                }
                                sSql = "update DOC_A1 SET DOC_A1_110 = '" + msfolder.Url + "', DOC_A1_40= " + msfolder.UserId + ", DOC_A1_30 = " + msfolder.Superiorid + " WHERE DOC_A1_10 =" + msfolder.FId;
                                DataAccessor.ExecuteNonQuery(sSql);
                                sSql = "update DOC_A2 SET DOC_A2_20 = " + msfolder.UserId;
                                DataAccessor.ExecuteNonQuery(sSql);
                            }
                            lstResult.Add(msfolder.FId + ";" + msfolder.FName + ";00");
                        }
                        else
                        {
                            lstResult.Add(msfolder.FId + ";" + msfolder.FName + ";-999");
                        }
                        #endregion
                    }
                    #endregion

                }
            }
            return lstResult;

        }

        #region 返回一行文件记录数据

        private MsFiles GetFiles(string sId)
        {
            MsFiles Ms = new MsFiles();
            string sSql = "select * from doc_a2 WHERE DOC_A2_10 = " + sId;
            IDataReader rd = DataAccessor.ExecuteReader(sSql);
            if (rd.Read())
            {
                Ms.folderId = rd["DOC_A2_30"].ToString().Trim();
                Ms.userId = rd["DOC_A2_20"].ToString().Trim();
                Ms.fileName = rd["DOC_A2_40"].ToString().Trim();
                Ms.filelength = rd["DOC_A2_60"].ToString().Trim();
                Ms.fileType = rd["DOC_A2_50"].ToString().Trim();
                Ms.remark = rd["DOC_A2_70"].ToString().Trim();
                Ms.updatetime = rd["DOC_A2_80"].ToString().Trim();

                Ms.jobId = rd["DOC_A2_9992"].ToString().Trim();
                Ms.levelId = rd["DOC_A2_9993"].ToString().Trim();
                Ms.deptId = rd["DOC_A2_9991"].ToString().Trim();
                Ms.agencId = rd["DOC_A2_9990"].ToString().Trim();
                Ms.cjdatetime = rd["DOC_A2_9995"].ToString().Trim();
                Ms.cjuserId = rd["DOC_A2_9994"].ToString().Trim();

            }
            return Ms;
        }
        #endregion

        #region 返回文件夹信息

        private MsFolder GetFolder(MsFolder Ms)
        {
            string sSql = string.Empty;
            sSql = "select * from DOC_A1 WHERE DOC_A1_10 = " + Ms.FId;
            IDataReader rd = DataAccessor.ExecuteReader(sSql);
            if (rd.Read())
            {
                Ms.FName = rd["DOC_A1_20"].ToString().Trim();
                Ms.Superiorid = rd["DOC_A1_30"].ToString().Trim();
                Ms.UserId = rd["DOC_A1_40"].ToString().Trim();
                Ms.Sharingmark = rd["DOC_A1_50"].ToString().Trim();
                Ms.Readmark = rd["DOC_A1_60"].ToString().Trim();
                Ms.ReadId = rd["DOC_A1_70"].ToString().Trim();
                Ms.CompleteyMark = rd["DOC_A1_80"].ToString().Trim();
                Ms.CompleteyId = rd["DOC_A1_90"].ToString().Trim();
                Ms.Utime = rd["DOC_A1_100"].ToString().Trim();

            }
            return Ms;
        }

        #endregion
        #endregion

        #region 获取目录下所有子节点ID
        public void GetLowLevFolderID(string higfolderid, ref string sConnectID)
        {
            if (sConnectID == "")
            {
                sConnectID = higfolderid + ",";
            }

            string sSql;
            sSql = "Select DOC_A1_10 from DOC_A1 where DOC_A1_30 IN (" + higfolderid + ")";
            DataSet dSet = DataAccessor.ExecuteDataSet(sSql);
            if (dSet.Tables[0].Rows.Count != 0)
            {
                higfolderid = "";
                for (int i = 0; i < dSet.Tables[0].Rows.Count; i++)
                {
                    sConnectID += dSet.Tables[0].Rows[i]["Doc_A1_10"].ToString().Trim();
                    sConnectID += ",";

                    higfolderid += dSet.Tables[0].Rows[i]["Doc_A1_10"].ToString().Trim();
                    higfolderid += ",";
                }
                GetLowLevFolderID(higfolderid.Trim(','), ref sConnectID);
            }
        }
        #endregion

        #region 返回所有他人共享文件夹的用户

        public List<string> GetOthPersons(string sQuery)
        {
            string sSql;
            List<string> lstResult = new List<string>();
            sSql = "select distinct  userName,userId from DOC_A1 A ";
            sSql += "left join v_udp on  USERID = DOC_A1_40  where 1=1 ";
            sSql += sQuery;
            IDataReader iReader = DataAccessor.ExecuteReader(sSql);
            while (iReader.Read())
            {
                lstResult.Add(iReader["userId"].ToString() + "," + iReader["userName"].ToString());
            }
            return lstResult;

        }
        #endregion

        #region 复制文件夹及其子文件夹与文件
        private void CodeFolder(DataView dv, string UserId, string Superiorid)
        {
            string sSql;
            foreach (DataRowView dr in dv)
            {
                #region 新增文件夹

                MsFolder ms = new MsFolder();
                ms.FId = dr["DOC_A1_10"].ToString().Trim();
                ms = GetFolder(ms);
                ms.UserId = UserId;
                ms.Superiorid = Superiorid;

                DataSet dSet = GetPersonDocfolder(ms, " and  DOC_A1_30 = " + ms.Superiorid, "1");
                if (dSet.Tables[0].Rows.Count != 0)
                {
                    ms.Url = dSet.Tables[0].Rows[0]["DOC_A1_110"].ToString().Trim() + "/" + dSet.Tables[0].Rows[0]["DOC_A1_20"].ToString().Trim();
                }

                ms.FId = "";
                List<string> lstResult = DoSaveFloderMess(ms);

                #region 新增文件夹下边所有文件

                sSql = "select DOC_A2_10 FROM DOC_A2 WHERE DOC_A2_30 = " + dr["DOC_A1_10"].ToString().Trim();
                dSet = DataAccessor.ExecuteDataSet(sSql);
                if (dSet.Tables[0].Rows.Count != 0)
                {
                    for (int i = 0; i < dSet.Tables[0].Rows.Count; i++)
                    {
                        MsFiles msfile = GetFiles(dSet.Tables[0].Rows[i]["DOC_A2_10"].ToString().Trim());
                        msfile.userId = UserId;    //文件所属用户

                        msfile.cjuserId = UserId;   //文件创建人

                        msfile.folderId = lstResult[0];    //所属文件夹
                        insertFile(msfile);
                        MsDocVersion msversion = new MsDocVersion();
                        msversion.Id = lstResult[0];
                        msversion.versionId = "1";
                        InsertVersion(msversion, dSet.Tables[0].Rows[i]["DOC_A2_10"].ToString().Trim());
                        msfile = null;
                    }
                }
                #endregion
                #endregion
                sSql = "select * from DOC_A1 WHERE DOC_A1_30 = " + dr["DOC_A1_10"];
                dSet = DataAccessor.ExecuteDataSet(sSql);
                if (dSet.Tables[0].Rows.Count != 0)
                {
                    DataView dvSun = new DataView(dSet.Tables[0]);
                    CodeFolder(dvSun, UserId, ms.FId);
                }
            }
        }
        #endregion


        #region IPersonDoc 成员
        /// <summary>
        /// 空间用户信息
        /// </summary>
        /// <returns></returns>
        public string DoOperateSpaceUser()
        {
            List<string> lstUserCount = GetUserId();
            if (lstUserCount.Count > 0)
            {
                List<string> lstCmdText = new List<string>();
                string cmdText = "";
                ConfigLib oConfig = new ConfigLib();
                string PERSONALSPACE = oConfig.GetAttribute("CCIT_SYSTEM/CCIT_PERSONALSPACE", eValueType.TEXT);//空间大小
                for (int i = 0; i < lstUserCount.Count; i++)
                {
                    string userId = GetSpaceUser(lstUserCount[i]);
                    if (userId == "")
                    {
                        cmdText = " insert into DOC_A3(DOC_A3_10,DOC_A3_20) values(" + lstUserCount[i] + "," + PERSONALSPACE + ")";
                        lstCmdText.Add(cmdText);
                    }
                }
                if (lstCmdText.Count > 0)
                {
                    int iNum = DataAccessor.ExecuteNonQuery(lstCmdText);
                    if (iNum > 0)
                    { return "1"; }
                    else
                    { return "-999"; }
                }
                else
                { return ""; }
            }
            else
            { return ""; }
        }

        #endregion
    }
}
